package com.dwd.user.controller;

import com.dwd.basic.utils.AjaxResult;
import com.dwd.org.pojo.Employee;
import com.dwd.user.pojo.User;
import com.dwd.user.pojo.dto.LoginInfoDto;
import com.dwd.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/16 12:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @PostMapping("/checkPhone")
    public AjaxResult checkRegistPhone(@RequestBody User user) {
        try {
            userService.checkPhone(user.getPhone());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 手机注册
     * @param loginInfoDto
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody LoginInfoDto loginInfoDto) {
        try {
            userService.register(loginInfoDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 邮箱注册
     * @param loginInfoDto
     * @return
     */
    @PostMapping("/emailRegister")
    public AjaxResult emailRegister(@RequestBody LoginInfoDto loginInfoDto) {
        try {
            userService.emailRegister(loginInfoDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }


    /**
     * 扩展登陆
     *
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Employee employee) {
        try {
            Employee loginUser = userService.login(employee.getUsername(), employee.getPassword());
            return AjaxResult.me().setResultObj(loginUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

}
